package com.mlk.engSystem.service.impl;

import com.mlk.core.component.page.Page;
import com.mlk.core.component.page.Pager;
import com.mlk.core.util.ServerResponse;
import com.mlk.engSystem.mapper.EngSysParamMapper;
import com.mlk.engSystem.po.EngSysParam;
import com.mlk.engSystem.service.EngSysParamService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Service
public class EngSysParamServiceImpl implements EngSysParamService {

    @Resource
    private EngSysParamMapper engSysParamMapper;

    @Override
    public String getValue(String key) {
        return this.engSysParamMapper.selectValueByKey(key);
    }

    @Override
    public int getIntValue(String key) {
        return Integer.parseInt(getValue(key));
    }

    @Override
    public long getLongValue(String key) {
        return Long.parseLong(getValue(key));
    }

    @Override
    public Double getDoubleValue(String key){
        return Double.parseDouble(getValue(key));
    }

    @Override
    public BigDecimal getBigDecimalValue(String key) {
        return BigDecimal.valueOf(getDoubleValue(key));
    }

    @Override
    @Page
    public Pager listPage() {
        return Pager.adapt(this.engSysParamMapper.selectList(null));
    }

    @Override
    public ServerResponse update(Integer id, String value) {
        return this.engSysParamMapper.updateById(EngSysParam.builder().id(id).value(value).build()) == 0 ?
                ServerResponse.fail("更新失败") : ServerResponse.ok("更新成功");
    }
}
